package com.sptokentest.service.ServiceImpl;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sptokentest.entity.Menu;
import com.sptokentest.entity.RoleMenu;
import com.sptokentest.entity.UserRole;
import com.sptokentest.mapper.MenuMapper;
import com.sptokentest.pj.dto.MenuPerDto;
import com.sptokentest.service.MenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sptokentest.service.RoleMenuService;
import com.sptokentest.service.UserRoleService;
import com.sptokentest.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author young
 * @since 2023年01月14日
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {
    @Autowired
    private UserService userService;
    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private RoleMenuService roleMenuService;
    @Autowired
    private MenuService menuService;

    /**
     * 获取当前用户的侧边菜单列表
     * @return
     */
    @Override
    public List<Menu> getmenus() {
        LambdaQueryWrapper<UserRole> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UserRole::getUserId, StpUtil.getLoginId());

        UserRole userRole = userRoleService.getOne(lqw);
        LambdaQueryWrapper<RoleMenu> lqw1 = new LambdaQueryWrapper<>();
        lqw1.eq(RoleMenu::getRoleId,userRole.getRoleId());
        List<RoleMenu> roleMenuList = roleMenuService.list(lqw1);
        List<Long> menuIds = new ArrayList<>();
        for (RoleMenu rolemenu :
                roleMenuList) {
            menuIds.add(rolemenu.getMenuId());
        }
        LambdaQueryWrapper<Menu> lqw2 = new LambdaQueryWrapper<>();
        lqw2.in(Menu::getMenuId,menuIds).isNull(Menu::getMid);
        return menuService.list(lqw2);
    }

    /**
     * 角色管理中获取对应用户的所有的权限，返回结果是一个数组
     * @param roleId
     * @return
     */
    @Override
    public SaResult getRolePer(Long roleId) {
        List<Long> perIds = new ArrayList<>();

        // 1. 首先查询role_menu表中 roleId = roleId的所有RoleMenu
        LambdaQueryWrapper<RoleMenu> lqw1 = new LambdaQueryWrapper<>();
        lqw1.eq(RoleMenu::getRoleId,roleId);
        List<RoleMenu> roleMenuList = roleMenuService.list(lqw1);
        // 2.
        List<Long> menuIds = new ArrayList<>();
        for (RoleMenu rolemenu :
                roleMenuList) {
            menuIds.add(rolemenu.getMenuId());
        }
        LambdaQueryWrapper<Menu> lqw2 = new LambdaQueryWrapper<>();
        lqw2.in(Menu::getMenuId,menuIds).isNotNull(Menu::getMid);
        List<Menu> pers = menuService.list(lqw2);
        for (Menu per :
                pers) {
            perIds.add(per.getMenuId());
        }
        return SaResult.data(perIds);
    }

    @Override
    public SaResult getPerMenus() {
        List<MenuPerDto> list = new ArrayList<>();
        LambdaQueryWrapper<Menu> lqw = new LambdaQueryWrapper<>();
        lqw.isNull(Menu::getMid);
        List<Menu> oneMenuList = menuService.list(lqw);

        for (Menu menu :
                oneMenuList) {
            MenuPerDto menuOneDto = new MenuPerDto();
            BeanUtils.copyProperties(menu,menuOneDto);
            LambdaQueryWrapper<Menu> lqw1 = new LambdaQueryWrapper<>();
            lqw1.eq(Menu::getMid,menu.getMenuId());
            List<Menu> twoMenuList = menuService.list(lqw1);

            List<Menu> childrenList = new ArrayList<>();
            for (Menu menu1 :
                    twoMenuList) {
                MenuPerDto menuTwoDto = new MenuPerDto();
                BeanUtils.copyProperties(menu1,menuTwoDto);
                childrenList.add(menuTwoDto);
            }
            menuOneDto.setChildren(childrenList);
            list.add(menuOneDto);
        }
        return SaResult.data(list);
    }
}
